import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Layout from '@/layout/index.vue'
import store from '@/store';

Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: 'login' */ '@/views/login/index.vue')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'home',
        // 指定路由懒加载生成的包名
        component: () => import(/* webpackChunkName: 'home' */ '@/views/home/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/advert',
        name: 'advert',
        component: () => import(/* webpackChunkName: 'advert' */ '@/views/advert/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/advert-space',
        name: 'advert-space',
        component: () => import(/* webpackChunkName: 'advert-space' */ '@/views/advert-space/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/course',
        name: 'course',
        component: () => import(/* webpackChunkName: 'course' */ '@/views/course/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/resource',
        name: 'resource',
        component: () => import(/* webpackChunkName: 'resource' */ '@/views/resource/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/role',
        name: 'role',
        component: () => import(/* webpackChunkName: 'role' */ '@/views/role/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName: 'user' */ '@/views/user/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/menu',
        name: 'menu',
        component: () => import(/* webpackChunkName: 'menu' */ '@/views/menu/index.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/menu/create',
        name: 'menu-create',
        component: () => import(/* webpackChunkName: 'menu-create' */ '@/views/menu/create.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/menu/:id/edit',
        name: 'menu-edit',
        component: () => import(/* webpackChunkName: 'menu-edit' */ '@/views/menu/edit.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        }
      },
      {
        path: '/role/:roleId/alloc-menu',
        name: 'alloc-menu',
        component: () => import(/* webpackChunkName: 'alloc-menu' */ '@/views/role/alloc-menu.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        },
        props: true, // 将路由路径参数映射到props中 命名需要一致
      },
      {
        path: '/course/new',
        name: 'create-course',
        component: () => import(/* webpackChunkName: 'create-course' */ '@/views/course/create.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        },
        props: true, // 将路由路径参数映射到props中 命名需要一致
      },
      {
        path: '/course/:courseId/edit',
        name: 'edit-course',
        component: () => import(/* webpackChunkName: 'create-edit' */ '@/views/course/editor.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        },
        props: true, // 将路由路径参数映射到props中 命名需要一致
      },
      {
        path: '/course/:courseId/section',
        name: 'section-course',
        component: () => import(/* webpackChunkName: 'create-section' */ '@/views/course/section.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        },
        props: true, // 将路由路径参数映射到props中 命名需要一致
      },
      {
        path: '/course/:courseId/video',
        name: 'video-course',
        component: () => import(/* webpackChunkName: 'create-video' */ '@/views/course/video.vue'),
        meta: {
          // 自定义数据，meta默认为空对象
          auth: true
        },
        props: true, // 将路由路径参数映射到props中 命名需要一致
      }
    ]
  },
  {
    path: '*',
    name: '404',
    component: () => import(/* webpackChunkName: 'error-page' */ '@/views/error-page/index.vue')
  }
]

const router = new VueRouter({
  routes
})

/**
 * 全局前置守卫，任何页面访问都需要经过这里
 * to 要去哪里的路由信息
 * from 从哪里来的路由信息
 * next 通行的标志
 */
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.auth)) {
    if (store.state.user) {
      next();
    } else {
      next({
        name: 'login',
        query: {
          redirect: to.fullPath // 把登录成功需要返回的页面告诉登录页面
        }
      });
    }
  } else {
    next();
  }
  // 路由守卫一定要执行next进行通行 不然页面为空
  // next();
})
export default router
